#include "maincontroller.h"

const int MainController::ANIMATION_DESPLAY_DELAY = 70;

MainController::MainController(QObject* parent):
    QObject(parent),
    m_timer(NULL),
    m_curFrame(0),
    m_maxFrame(0)
{
    initModel();
    setupConnect();
}

MainController::~MainController()
{
    m_plistfileNameList.clear();
    CC_SAFE_DELETE_NULL(m_plistParse);
    CC_SAFE_DELETE_NULL(m_sourceFrameModel);    
    CC_SAFE_DELETE_NULL(m_actionModel);
    CC_SAFE_DELETE_NULL(m_frameModel);
    CC_SAFE_DELETE_NULL(m_framegraphicsModel);
}

void MainController::initModel()
{
    m_plistParse = new PlistParser();
    m_sourceFrameModel = new SourceFrameModel(m_plistParse);

    m_actionModel = new ActionModel(AnimationDataHandler::instance());
    m_frameModel = new FrameModel(AnimationDataHandler::instance());

    m_framegraphicsModel = new GraphicsFrameModel(AnimationDataHandler::instance());

}

void MainController::setupConnect()
{
    //data
    connect(AnimationDataHandler::instance(), SIGNAL(notifyAllDataChangeUpdate()),m_actionModel, SLOT(dataChangeUpdate()));
    connect(AnimationDataHandler::instance(), SIGNAL(notifyAllDataChangeUpdate()),m_frameModel, SLOT(dataChangeUpdate()));
    connect(AnimationDataHandler::instance(), SIGNAL(notifyAllDataChangeUpdate()),m_framegraphicsModel, SLOT(dataChangeUpdate()));

    connect(AnimationDataHandler::instance(), SIGNAL(notifyActionDataChangeUpdate()), m_frameModel,SLOT(dataChangeUpdate()));
    connect(AnimationDataHandler::instance(), SIGNAL(notifyActionDataChangeUpdate()), m_framegraphicsModel,SLOT(dataChangeUpdate()));

    connect(AnimationDataHandler::instance(), SIGNAL(notifyFrameDataChangeUpdate()), m_framegraphicsModel,SLOT(dataChangeUpdate()));

    //plist
    connect(m_plistParse, SIGNAL(notifyPlistDataChanged()), m_sourceFrameModel, SLOT(dataChangeUpdate()));

}

void MainController::setFrameGraphicsView(QGraphicsView *graphicsView)
{
    m_frameGraphicsView = graphicsView;
    m_frameGraphicsView->setScene(m_framegraphicsModel->getScene());
    m_framegraphicsModel->setViewRect(m_frameGraphicsView->rect());

}

void MainController::setFrameListView(QTreeView *listView)
{
    m_frameListView = listView;
    m_frameListView->setModel(m_frameModel);

    m_frameSelectionModel = m_frameListView->selectionModel();
    QModelIndex index = m_frameModel->getCurrentFrameIndex();
    QItemSelection seletion(index, index);
    m_frameSelectionModel->select(seletion,QItemSelectionModel::Select);

    connect(m_frameSelectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)), m_frameModel, SLOT(selectChanged(QItemSelection,QItemSelection)));
}

void MainController::setActionListView(QTreeView* listView)
{
    m_actionListView = listView;
    m_actionListView->setModel(m_actionModel);

    QModelIndex index = m_actionModel->getCurrentActionIndex();
    m_actionSelectModel = m_actionListView->selectionModel();

    QItemSelection seletion(index, index);
    m_actionSelectModel->select(seletion,QItemSelectionModel::Select);

    connect(m_actionSelectModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)), m_actionModel, SLOT(selectChanged(QItemSelection,QItemSelection)));
}

void MainController::createNewAction()
{    
    m_actionModel->appendNewAction();
}

void MainController::deleteAction()
{
    QModelIndex currentIndex = m_actionListView->currentIndex();
    m_actionModel->deleteAction(currentIndex);
}

void MainController::addFrameToAction()
{
    QModelIndexList indexList = m_sourceSelectionModel->selectedIndexes();
    if(indexList.count() > 0)
    {
        PlistFrameInfoList infoList;
        for(int i = 0;i < indexList.count(); i ++)
        {            
            QModelIndex index = indexList.at(i);
            PlistFrameInfo* info = m_sourceFrameModel->getInfoData(index);
            infoList.append(info);
        }

        m_frameModel->appendFrameList(infoList);
    }
    m_sourceSelectionModel->clear();
}

void MainController::removeFrame()
{
    QModelIndexList indexList = m_frameSelectionModel->selectedIndexes();
    if(indexList.count() > 0)
    {
        QModelIndex index = indexList.at(0);
        m_frameModel->removeFrameByIndexList(index);
    }
}

//frameListView
void MainController::setSourceFrameListView(QListView *listView)
{
    m_sourceframeListView = listView;
    m_sourceframeListView->setModel(m_sourceFrameModel);
    m_sourceSelectionModel = m_sourceframeListView->selectionModel();

}

bool MainController::parsePlistFile(const QString &fileName)
{
    setWorkDir(fileName);

     return m_plistParse->parsePlistFile(fileName);
}


void MainController::recordPlistInfo(const QString &fileName)
{
    m_plistfileNameList.append(fileName);
}


bool MainController::saveDataToFile(const QString &fileName)
{
    setWorkDir(fileName);
     const QDomDocument* doc = AnimationDataHandler::instance()->convertToDomDocument();
     return UtilitFile::saveDomToFile(doc, fileName);

     CC_SAFE_DELETE_NULL(doc);
}

bool MainController::loadDataFromFile(const QString& fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    setWorkDir(fileName);

    m_framegraphicsModel->setWorkingDir(m_workingDir);

    if(AnimationDataHandler::instance()->loadFromFile(file))
    {
         file.close();
         return true;
    }

    return false;
}


void MainController::setWorkDir(const QString &fileName)
{
    QString path = fileName;
    QString last = path.split("/").last();
    m_workingDir = path.remove(last) ;
    m_framegraphicsModel->setWorkingDir(m_workingDir);
}

void MainController::stopAnimation()
{
    stopTimer();
}

void MainController::playCurrentAnimation()
{
    stopTimer();

    Action* action = AnimationDataHandler::instance()->getCurrentAction();
    if(action == NULL) return;

    m_maxFrame = action->getFrameCount();
    m_curFrame = 0;

    m_timer = new QTimer();
    connect(m_timer,SIGNAL(timeout()),this,SLOT(playNextFrame()));
    m_timer->start(action->getDelay() * 1000);
}

void MainController::playNextFrame()
{
    if(m_curFrame == m_maxFrame)
    {
        stopTimer();
    }

    QModelIndex index = m_frameModel->getFrameModelIndexbyIndex(m_curFrame);
    QItemSelection selection(index,index);
    m_frameSelectionModel->clear();
    m_frameSelectionModel->select(selection, QItemSelectionModel::Select);
    m_curFrame++;
}

void MainController::stopTimer()
{
    if(m_timer != NULL)
    {
        if(m_timer->isActive()) m_timer->stop();
        CC_SAFE_DELETE_NULL(m_timer);
    }

}

